﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using ApplLogic.ApplClasses;
using ApplLogic.ApplDbMediators;
using ConnectionObjects;
using Utilities.GlobalData;

namespace ApplLogic.LogicalServices
{
    public class ApplLoginHandler
    {
        public string UserName { get; set; }
        public string Password { get; set; }
        private object m_LoggedInUser;
        private eWorkingWithServerType m_UserType;
        private TcpClient m_Client;

        public ApplLoginHandler(TcpClient client)
        {
            m_Client = client;
        }

        public eWorkingWithServerType HandleLogin()
        {
            try
            {
                do
                {
                    OutConnectionObject returnObject = new OutConnectionObject();
                    LoginObject loginData = (LoginObject) (new BinaryFormatter().Deserialize(m_Client.GetStream()));
                    Password = loginData.Password;
                    UserName = loginData.UserName;
                    verifyLogin();
                    if (isLoginSucessfull())
                    {
                        returnObject.RetVal = LoggedInUser;
                        returnObject.ExceptionMessage = null;
                        returnObject.OptionalMessage = "LoginOk";
                        m_UserType = LoggedInUser is ApplManager
                                         ? eWorkingWithServerType.Manager
                                         : eWorkingWithServerType.Worker;
                    }
                    else
                    {
                        returnObject.RetVal = null;
                        returnObject.ExceptionMessage = "Incorrect UserName or Password";
                        returnObject.OptionalMessage = "LoginFailed";
                    }
                    BinaryFormatter binaryFormatter = new BinaryFormatter();
                    binaryFormatter.Serialize(m_Client.GetStream(), returnObject);
                    m_Client.GetStream().Flush();
                } while (m_UserType == eWorkingWithServerType.None);
            }
            catch (Exception ex)
            {
                if (!m_Client.Connected)
                {
                    Console.WriteLine("Client is left the server while loging-in");
                }
            }

            return m_UserType;
        }

        private void verifyLogin()
        {
            ApplWorker worker = ApplDbWorkerMediator.Instance.GetWorker(UserName, Password);
            ApplManager manager = ApplDbManagerMediator.Instance.GetManager(UserName, Password);
            if(worker != null)
            {
                m_LoggedInUser = worker;
                m_UserType = eWorkingWithServerType.Worker;
            }
            else if(manager != null)
            {
                m_LoggedInUser = manager;
                m_UserType = eWorkingWithServerType.Manager;
            }
        }

        private bool isLoginSucessfull()
        {
            return m_LoggedInUser != null;
        }

        public object LoggedInUser { get { return m_LoggedInUser; } }

        public eWorkingWithServerType UserType { get { return m_UserType; } }
    }
}
